package com.dragon.myapplication;

import androidx.lifecycle.ViewModelProvider;

import android.animation.ObjectAnimator;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.dragon.myapplication.padings.PadingActivity;

public class FirstFragment extends Fragment {

    private FirstViewModel mViewModel;
    private ImageView imageView;
    private Button button;

    public static FirstFragment newInstance() {
        return new FirstFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view =  inflater.inflate(R.layout.first_fragment, container, false);
        imageView = view.findViewById(R.id.imageView);
        button = view.findViewById(R.id.button);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //由于owner设置了fragment所以切换fragment时会让fragment状态消失，所以这可以使用requireActivity()
        mViewModel = new ViewModelProvider(this).get(FirstViewModel.class);
        imageView.setRotation(mViewModel.rotationPosition);

        //属性动画  缩放scaleX scaleY
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(imageView,"rotation",0,0);
        objectAnimator.setDuration(500);
        imageView.setOnClickListener(v -> {
            if (! objectAnimator.isRunning()) {
                objectAnimator.setFloatValues(imageView.getRotation(), imageView.getRotation() + 100);
                mViewModel.rotationPosition += 100;
                objectAnimator.start();
            }
        });

        button.setOnClickListener( v -> startActivity(new Intent(requireActivity(), PadingActivity.class)));
    }

}